home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing;
-
- import com.sun.java.accessibility.Accessible;
- import com.sun.java.accessibility.AccessibleContext;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Graphics;
- import java.awt.LayoutManager;
- import java.awt.Rectangle;
- import java.util.Hashtable;
-
- public class JLayeredPane extends JComponent implements Accessible {
- public static final Integer DEFAULT_LAYER = new Integer(0);
- public static final Integer PALETTE_LAYER = new Integer(100);
- public static final Integer MODAL_LAYER = new Integer(200);
- public static final Integer POPUP_LAYER = new Integer(300);
- public static final Integer DRAG_LAYER = new Integer(400);
- public static final Integer FRAME_CONTENT_LAYER = new Integer(-30000);
- public static final String LAYER_PROPERTY = "layeredContainerLayer";
- private Hashtable componentToLayer;
- private boolean optimizedDrawingPossible = true;
-
- public JLayeredPane() {
- ((Container)this).setLayout((LayoutManager)null);
- }
-
- protected void addImpl(Component comp, Object constraints, int index) {
- int layer = DEFAULT_LAYER;
- if (constraints instanceof Integer) {
- layer = (Integer)constraints;
- this.setLayer(comp, layer);
- } else {
- layer = this.getLayer(comp);
- }
-
- int pos = this.insertIndexForLayer(layer, index);
- super.addImpl(comp, constraints, pos);
- comp.validate();
- comp.repaint();
- this.validateOptimizedDrawing();
- }
-
- public AccessibleContext getAccessibleContext() {
- if (super.accessibleContext == null) {
- super.accessibleContext = new AccessibleJLayeredPane(this);
- }
-
- return super.accessibleContext;
- }
-
- public int getComponentCountInLayer(int layer) {
- int layerCount = 0;
- int count = ((Container)this).getComponentCount();
-
- for(int i = 0; i < count; ++i) {
- int curLayer = this.getLayer(((Container)this).getComponent(i));
- if (curLayer == layer) {
- ++layerCount;
- } else if (layerCount > 0 || curLayer < layer) {
- break;
- }
- }
-
- return layerCount;
- }
-
- public Component[] getComponentsInLayer(int layer) {
- int layerCount = 0;
- Component[] results = new Component[this.getComponentCountInLayer(layer)];
- int count = ((Container)this).getComponentCount();
-
- for(int i = 0; i < count; ++i) {
- int curLayer = this.getLayer(((Container)this).getComponent(i));
- if (curLayer == layer) {
- results[layerCount++] = ((Container)this).getComponent(i);
- } else if (layerCount > 0 || curLayer < layer) {
- break;
- }
- }
-
- return results;
- }
-
- protected Hashtable getComponentToLayer() {
- if (this.componentToLayer == null) {
- this.componentToLayer = new Hashtable(4);
- }
-
- return this.componentToLayer;
- }
-
- public int getIndexOf(Component c) {
- int count = ((Container)this).getComponentCount();
-
- for(int i = 0; i < count; ++i) {
- if (c == ((Container)this).getComponent(i)) {
- return i;
- }
- }
-
- return -1;
- }
-
- public static int getLayer(JComponent c) {
- Integer i;
- return (i = (Integer)c.getClientProperty("layeredContainerLayer")) != null ? i : DEFAULT_LAYER;
- }
-
- public int getLayer(Component c) {
- Integer i;
- if (c instanceof JComponent) {
- i = (Integer)((JComponent)c).getClientProperty("layeredContainerLayer");
- } else {
- i = (Integer)this.getComponentToLayer().get("layeredContainerLayer");
- }
-
- return i == null ? DEFAULT_LAYER : i;
- }
-
- public static JLayeredPane getLayeredPaneAbove(Component c) {
- if (c == null) {
- return null;
- } else {
- Component parent;
- for(parent = c.getParent(); parent != null && !(parent instanceof JLayeredPane); parent = parent.getParent()) {
- }
-
- return (JLayeredPane)parent;
- }
- }
-
- protected Integer getObjectForLayer(int layer) {
- Integer layerObj;
- switch (layer) {
- case 0:
- layerObj = DEFAULT_LAYER;
- break;
- case 100:
- layerObj = PALETTE_LAYER;
- break;
- case 200:
- layerObj = MODAL_LAYER;
- break;
- case 300:
- layerObj = POPUP_LAYER;
- break;
- case 400:
- layerObj = DRAG_LAYER;
- break;
- default:
- layerObj = new Integer(layer);
- }
-
- return layerObj;
- }
-
- public int getPosition(Component c) {
- int pos = 0;
- ((Container)this).getComponentCount();
- int startLocation = this.getIndexOf(c);
- if (startLocation == -1) {
- return -1;
- } else {
- int startLayer = this.getLayer(c);
-
- for(int i = startLocation - 1; i >= 0; --i) {
- int curLayer = this.getLayer(((Container)this).getComponent(i));
- if (curLayer != startLayer) {
- return pos;
- }
-
- ++pos;
- }
-
- return pos;
- }
- }
-
- public int highestLayer() {
- return ((Container)this).getComponentCount() > 0 ? this.getLayer(((Container)this).getComponent(0)) : 0;
- }
-
- protected int insertIndexForLayer(int layer, int position) {
- int layerStart = -1;
- int layerEnd = -1;
- int count = ((Container)this).getComponentCount();
-
- for(int i = 0; i < count; ++i) {
- int curLayer = this.getLayer(((Container)this).getComponent(i));
- if (layerStart == -1 && curLayer == layer) {
- layerStart = i;
- }
-
- if (curLayer < layer) {
- if (i == 0) {
- layerStart = 0;
- layerEnd = 0;
- } else {
- layerEnd = i;
- }
- break;
- }
- }
-
- if (layerStart == -1 && layerEnd == -1) {
- return count;
- } else {
- if (layerStart != -1 && layerEnd == -1) {
- layerEnd = count;
- }
-
- if (layerEnd != -1 && layerStart == -1) {
- layerStart = layerEnd;
- }
-
- if (position == -1) {
- return layerEnd;
- } else {
- return position > -1 && layerStart + position <= layerEnd ? layerStart + position : layerEnd;
- }
- }
- }
-
- public boolean isOptimizedDrawingEnabled() {
- return this.optimizedDrawingPossible;
- }
-
- public int lowestLayer() {
- int count = ((Container)this).getComponentCount();
- return count > 0 ? this.getLayer(((Container)this).getComponent(count - 1)) : 0;
- }
-
- public void moveToBack(Component c) {
- this.setPosition(c, this.getComponentCountInLayer(this.getLayer(c)));
- }
-
- public void moveToFront(Component c) {
- this.setPosition(c, 0);
- }
-
- public void paint(Graphics g) {
- if (((JComponent)this).isOpaque()) {
- Rectangle r = g.getClipBounds();
- Color c = ((Component)this).getBackground();
- if (c == null) {
- c = Color.lightGray;
- }
-
- g.setColor(c);
- g.fillRect(r.x, r.y, r.width, r.height);
- }
-
- super.paint(g);
- }
-
- public static void putLayer(JComponent c, int layer) {
- Integer layerObj = new Integer(layer);
- c.putClientProperty("layeredContainerLayer", layerObj);
- }
-
- public void remove(int index) {
- ((Container)this).getComponent(index);
- super.remove(index);
- this.validateOptimizedDrawing();
- }
-
- public void setLayer(Component c, int layer) {
- this.setLayer(c, layer, -1);
- }
-
- public void setLayer(Component c, int layer, int position) {
- Integer layerObj = this.getObjectForLayer(layer);
- if (layer == this.getLayer(c) && position == this.getPosition(c)) {
- if (c instanceof JComponent) {
- ((JComponent)this).repaint(((JComponent)c)._bounds);
- } else {
- ((JComponent)this).repaint(c.getBounds());
- }
-
- } else {
- if (c instanceof JComponent) {
- ((JComponent)c).putClientProperty("layeredContainerLayer", layerObj);
- } else {
- this.getComponentToLayer().put("layeredContainerLayer", layerObj);
- }
-
- if (c.getParent() != null && c.getParent() == this) {
- ((Container)this).remove(c);
- ((Container)this).add(c, (Object)null, position);
- if (c instanceof JComponent) {
- ((JComponent)this).repaint(((JComponent)c)._bounds);
- } else {
- ((JComponent)this).repaint(c.getBounds());
- }
-
- } else {
- if (c instanceof JComponent) {
- ((JComponent)this).repaint(((JComponent)c)._bounds);
- } else {
- ((JComponent)this).repaint(c.getBounds());
- }
-
- }
- }
- }
-
- public void setPosition(Component c, int position) {
- this.setLayer(c, this.getLayer(c), position);
- }
-
- private void validateOptimizedDrawing() {
- boolean layeredComponentFound = false;
- synchronized(((Component)this).getTreeLock()){}
-
- try {
- Integer layer = null;
- int i = 0;
-
- for(int d = ((Container)this).getComponentCount(); i < d; ++i) {
- layer = null;
- if ((((Container)this).getComponent(i) instanceof JInternalFrame || ((Container)this).getComponent(i) instanceof JComponent && (layer = (Integer)((JComponent)((Container)this).getComponent(i)).getClientProperty("layeredContainerLayer")) != null) && (layer == null || !layer.equals(FRAME_CONTENT_LAYER))) {
- layeredComponentFound = true;
- break;
- }
- }
- } catch (Throwable var7) {
- throw var7;
- }
-
- if (layeredComponentFound) {
- this.optimizedDrawingPossible = false;
- } else {
- this.optimizedDrawingPossible = true;
- }
-
- }
- }
-